﻿using System.Collections;
using System.Collections.Generic;
using GameFramework.Fsm;
using UnityEngine;

namespace StarForce {
    public abstract class BasicCardState : FsmState<Card> {
        private const int LayerRenderNormal = 0;
        private const int LayerRenderTop = 1;       // 更大的RenderLayer将会被更上面渲染
        protected UiCardParameters Parameters { get; }
        protected bool IsInitialized { get; }
        protected Card Hander { get; private set; }
        protected IFsm<Card> Fsm { get; private set; }

        protected BasicCardState(Card handler,UiCardParameters parameters) {
            Parameters = parameters;
            IsInitialized = true;
        }

        protected override void OnEnter(IFsm<Card> fsm) {
            base.OnEnter(fsm);
            Fsm = fsm;
            Hander = fsm.Owner;    
        }

        protected override void OnLeave(IFsm<Card> fsm, bool isShutdown) {
            base.OnLeave(fsm, isShutdown);
            Fsm = null;
            Hander = null;
        }

        protected virtual void MakeRenderFirst() {
            foreach (var spriteRenderer in Hander.SpriteRenderers) {
                spriteRenderer.sortingOrder = LayerRenderTop;
            }
        }

        protected virtual void MakeRenderNormal() {
            foreach (var spriteRenderer in Hander.SpriteRenderers) {
                spriteRenderer.sortingOrder = LayerRenderNormal;
            }
        }

        protected void EnableCollision() => Hander.Collider.enabled = true;
        protected void DisableCollision() => Hander.Collider.enabled = false;
        protected void RemoveAllTransparency() {
            foreach (var renderer in Hander.SpriteRenderers) {
                if (renderer) {
                    Color myColor = renderer.color;
                    myColor.a = 1;
                    renderer.color = myColor;
                }
            }
        }

        protected void DisableAllTransparency() {
            foreach (var renderer in Hander.SpriteRenderers) {
                if (renderer) {
                    Color myColor = renderer.color;
                    myColor.a = Parameters.DisableAlpha;
                    renderer.color = myColor;
                }
            }
        }

        protected virtual void Enable() {
            if (Hander.Collider) {
                EnableCollision();
            }

            if (Hander.Rigidbody) {
                Hander.Rigidbody.Sleep();
            }

            MakeRenderNormal();
            RemoveAllTransparency();
        }

        protected virtual void Disable() {
            DisableCollision();
            Hander.Rigidbody.Sleep();
            MakeRenderNormal();
            DisableAllTransparency();
        }



    }
}

